package com.ruoyi.project.shortUrl.url.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.shortUrl.url.domain.DomainOfShortUrl;
import com.ruoyi.project.shortUrl.url.service.IDomainOfShortUrlService;

/**
 * 短链接域名Controller
 * 
 * @author ruoyi
 * @date 2020-04-05
 */
@Controller
@RequestMapping("/short/domain")
public class DomainOfShortUrlController extends BaseController
{
    private String prefix = "shortUrl/domain";

    @Autowired
    private IDomainOfShortUrlService domainOfShortUrlService;

    @RequiresPermissions("short:domain:view")
    @GetMapping()
    public String url()
    {
        return prefix + "/url";
    }

    /**
     * 查询短链接域名列表
     */
    @RequiresPermissions("short:domain:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DomainOfShortUrl domainOfShortUrl)
    {
        startPage();
        List<DomainOfShortUrl> list = domainOfShortUrlService.selectList(domainOfShortUrl);
        return getDataTable(list);
    }

    /**
     * 导出短链接域名列表
     */
    @RequiresPermissions("short:domain:export")
    @Log(title = "短链接域名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DomainOfShortUrl domainOfShortUrl)
    {
        List<DomainOfShortUrl> list = domainOfShortUrlService.selectList(domainOfShortUrl);
        ExcelUtil<DomainOfShortUrl> util = new ExcelUtil<DomainOfShortUrl>(DomainOfShortUrl.class);
        return util.exportExcel(list, "url");
    }

    /**
     * 新增短链接域名
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存短链接域名
     */
    @RequiresPermissions("short:domain:add")
    @Log(title = "短链接域名", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DomainOfShortUrl domainOfShortUrl)
    {
		if(domainOfShortUrlService.selectDomainUnique(domainOfShortUrl.getDomain())>0) {
        	throw new BusinessException("域名重复了！");
        }
        return toAjax(domainOfShortUrlService.insertDomainOfShortUrl(domainOfShortUrl));
    }

    /**
     * 修改短链接域名
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DomainOfShortUrl domainOfShortUrl = domainOfShortUrlService.selectDomainOfShortUrlById(id);
        mmap.put("domainOfShortUrl", domainOfShortUrl);
        return prefix + "/edit";
    }

    /**
     * 修改保存短链接域名
     */
    @RequiresPermissions("short:domain:edit")
    @Log(title = "短链接域名", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DomainOfShortUrl domainOfShortUrl)
    {
        return toAjax(domainOfShortUrlService.updateDomainOfShortUrl(domainOfShortUrl));
    }

    /**
     * 删除短链接域名
     */
    @RequiresPermissions("short:domain:remove")
    @Log(title = "短链接域名", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(domainOfShortUrlService.deleteDomainOfShortUrlByIds(ids));
    }
}